package rest;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import dao.ClientDAO;
import dao.JPA;
import entity.Client;

@Path("/clientService")
@Stateless
public class ClientService {

	@Inject @JPA
	private ClientDAO clientDAO;
	
	@GET
	@Path("/findAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String findAllClients() {
		String res = "";
		for(Client client : clientDAO.findAll()) {
			res += client.getFirstName() + client.getLastName() + "\n";
		}
		return res;
	}
	
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/find/{clientId}")
	public String findClient(@PathParam("clientId") int clientId) {
		Client client = clientDAO.getEntityById(clientId);
		return client.getFirstName() + client.getLastName();
	}
	
	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/create")
	public void create(Client client) {
		clientDAO.persist(client);
	}

	@DELETE
	@Path("/remove/{clientId}")
	public void remove(@PathParam("clientId") int clientId) {
		clientDAO.remove(clientDAO.getEntityById(clientId));
	}

}